// AcEdExt.cpp: implementation of the AcEdExt class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"
#include "AcEdExt.h"
#include <geassign.h>
#include <dbxutil.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

namespace AcEdExt
{

bool GetPointWithoutDefaultValue( AcGePoint3d& pt, const char* prompt )
{
	int state = acedGetPoint( NULL, prompt, asDblArray( pt ) );
	return ( state == RTNORM ) ? true : false;
}

bool GetPointWithDefaultValue( AcGePoint3d& pt, const char* prompt )
{
	int state = acedGetPoint( NULL, prompt, asDblArray( pt ) );
	return ( state == RTNORM || state == RTNONE ) ? true : false;
}

bool GetVectorWithoutDefaultValue( AcGeVector3d& v, const char* prompt )
{
	int state = acedGetPoint( asDblArray( AcGePoint3d::kOrigin ), prompt, asDblArray( v ) );
	return ( state == RTNORM ) ? true : false;
}

bool GetVectorWithDefaultValue( AcGeVector3d& v, const char* prompt )
{
	int state = acedGetPoint( asDblArray( AcGePoint3d::kOrigin ), prompt, asDblArray( v ) );
	return ( state == RTNORM || state == RTNONE ) ? true : false;
}

void GetCoordSystemInWCSByZYAxes( AcGeMatrix3d& cs, const char* promptPt, const char* promptZ, const char* promptY )
{
	AcGePoint3d pt;
	if ( !GetPointWithDefaultValue( pt, promptPt ) ) return;
	acdbUcs2Wcs( asDblArray( pt ), asDblArray( pt ), false );

	AcGeMatrix3d ucs;
	ucs.setCoordSystem( pt, AcGeVector3d::kXAxis, AcGeVector3d::kZAxis, -AcGeVector3d::kYAxis );
	acedSetCurrentUCS( ucs );

	AcGeVector3d vz( AcGeVector3d::kYAxis );
	if ( !GetVectorWithoutDefaultValue( vz, promptZ ) )
	{
		acedSetCurrentUCS( AcGeMatrix3d::kIdentity );
		return;
	}
	acdbUcs2Wcs( asDblArray( vz ), asDblArray( vz ), true );

	AcGeVector3d vy( AcGeVector3d::kXAxis );
	vy.rotateBy( AcGeVector3d::kZAxis.angleTo( vz, AcGeVector3d::kYAxis ), AcGeVector3d::kYAxis );	

	ucs.setCoordSystem( pt, vy, vz.crossProduct( vy ), vz );
	acedSetCurrentUCS( ucs );

	vy = AcGeVector3d::kYAxis;
	if ( !GetVectorWithoutDefaultValue( vy, promptY ) )
	{
		acedSetCurrentUCS( AcGeMatrix3d::kIdentity );
		return;
	}
	acdbUcs2Wcs( asDblArray( vz ), asDblArray( vz ), true );

	acedSetCurrentUCS( AcGeMatrix3d::kIdentity );

	cs.setCoordSystem( pt, vy.crossProduct( vz ), vy, vz );
}


}
